package com.fadadianqi.logistics.admin.filter;

import com.yeqiangwei.commons.util.StringHelper;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.apache.log4j.Logger;
import org.springframework.context.MessageSource;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class Navigation {

    private static final Logger logger = Logger.getLogger(Navigation.class);

    private MessageSource messageSource;

    private List<Menu> menuList;

    private Map<String, Boolean> permissionURI;

    public void setPermissionURI(Map<String, Boolean> permissionURI) {
        this.permissionURI = permissionURI;
    }

    private boolean hasPermission(String uri) {
        if (permissionURI == null) {
            return Boolean.TRUE;
        }
        Boolean b = permissionURI.get(uri);
        if (b == null) {
            return Boolean.FALSE;
        }
        return b;
    }

    public String toString(String URI) {
        StringBuilder sb = new StringBuilder();
        if (!ValidatorHelper.isEmpty(menuList)) {
            for (Menu menu : menuList) {
                if (!hasPermission(menu.getURL())) {
                    // 无权访问 则不显示这个链接
                    continue;
                }
                String liKey = menu.getMessageKey().replace(".", "_");
                if (menu.getUriMap().containsKey(URI)) {
                    sb.append("<li class=\"yyy_slt\" id=\"" + liKey + "\">");
                } else {
                    if (!ValidatorHelper.isEmpty(menu.getUriPrefix())) {
                        boolean isOrNo = false;
                        Iterator<String> it = menu.getUriPrefix().iterator();
                        while (it.hasNext()) {
                            String tmp = it.next();
                            logger.debug("......" + tmp);
                            if (URI.startsWith(tmp)) {
                                isOrNo = true;
                                break;
                            } else if (tmp.indexOf("*") != -1) {
                                isOrNo = true;
                                String[] strs = StringHelper.stringToStrings(tmp, "*");
                                for (String u : strs) {
                                    logger.debug("......" + u);
                                    if (URI.indexOf(u) == -1) {
                                        isOrNo = false;
                                        break;
                                    }
                                }
                            }
                        }
                        if (isOrNo) {
                            sb.append("<li class=\"yyy_slt\" id=\"" + liKey + "\">");
                        } else {
                            sb.append("<li id=\"" + liKey + "\">");
                        }
                    } else {
                        sb.append("<li> id=\"" + liKey + "\">");
                    }
                }
                sb.append("<a href=\"");
                sb.append(menu.getURL());
                sb.append("\">");
                sb.append(messageSource.getMessage(menu.getMessageKey(), new Object[]{}, Locale.getDefault()));
                sb.append("</a>");
                sb.append("</li>");
            }
        }
        return sb.toString();
    }

    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }

    public MessageSource getMessageSource() {
        return messageSource;
    }

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

/*	private HttpServletRequest request;

	public Navigation(HttpServletRequest request) {
		this.request = request;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		String uri = request.getRequestURI();
		sb.append("<ul>");
		List<Nav> list = new ArrayList<Nav>();
		Nav store = new Nav("库存管理", "/a/store/list");
		Nav sell = new Nav("销售管理", "/a/sell/list");
		Nav village = new Nav("村庄管理", "/a/village/list");
		Nav customer = new Nav("顾客信息", "/a/customer/list");
		Nav brand = new Nav("品牌分类", "/a/brand-category/list");
		Nav goods = new Nav("商品管理", "/a/goods/list");
		Nav inOut = new Nav("流水记录", "/a/in-out/list");
		Nav stat = new Nav("统计分析", "/a/stat/");
		list.add(store);
		list.add(sell);
		list.add(village);
		list.add(customer);
		list.add(brand);
		list.add(goods);
		list.add(inOut);
		list.add(stat);
		for(Nav nav : list) {
			sb.append("<li");
			int e = nav.uri.lastIndexOf("/");
			String s = StringHelper.substring(nav.uri, 0, e, null);
			if(uri.indexOf(s) != -1) {
				sb.append(" class='yyy_slt'");
			}
			sb.append("><a href=\"" + nav.uri + "\">" + nav.name + "</a></li>");
		}
		sb.append("</ul>");
		return sb.toString();
	}
	
	private class Nav {
		
		String name;
		
		String uri;
		
		Nav(String name, String uri) {
			this.name = name;
			this.uri = uri;
		}
	}*/
}
